package com.github.cakin.shiro.chapter16.dao;

import com.github.cakin.shiro.chapter16.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @className: UserDaoImpl
 * @description: UserDaoImpl
 * @date: 2020/5/27
 * @author: cakin
 */

@Repository
public class UserDaoImpl implements UserDao {

    /**
     * 注入jdbcTemplate
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 功能描述：创建用户
     *
     * @param user 用户
     * @return User 用户
     * @author cakin
     * @date 2020/5/19
     */
    public User createUser( final User user ) {
        final String sql = "insert into sys_user(organization_id, username, password, salt, role_ids, locked) values(?,?,?,?,?,?)";

        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement( Connection connection ) throws SQLException {
                PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"});
                int count = 1;
                psst.setLong(count++, user.getOrganizationId());
                psst.setString(count++, user.getUsername());
                psst.setString(count++, user.getPassword());
                psst.setString(count++, user.getSalt());
                psst.setString(count++, user.getRoleIdsStr());
                psst.setBoolean(count++, user.getLocked());
                return psst;
            }
        }, keyHolder);

        user.setId(keyHolder.getKey().longValue());
        return user;
    }

    /**
     * 功能描述：更新用户
     *
     * @param user 用户
     * @return User 用户
     * @author cakin
     * @date 2020/5/19
     */
    public User updateUser( User user ) {
        String sql = "update sys_user set organization_id=?,username=?, password=?, salt=?, role_ids=?, locked=? where id=?";
        jdbcTemplate.update(
                sql,
                user.getOrganizationId(), user.getUsername(), user.getPassword(), user.getSalt(), user.getRoleIdsStr(),
                user.getLocked(), user.getId());
        return user;
    }

    /**
     * 功能描述：根据用户Id删除用户
     *
     * @param userId 用户Id
     * @author cakin
     * @date 2020/5/19
     */
    public void deleteUser( Long userId ) {
        String sql = "delete from sys_user where id=?";
        jdbcTemplate.update(sql, userId);
    }

    /**
     * 功能描述：根据用户Id查找用户
     *
     * @param userId 用户Id
     * @return User 用户
     * @author cakin
     * @date 2020/5/19
     */
    public User findOne( Long userId ) {
        String sql = "select id, organization_id, username, password, salt, role_ids as roleIdsStr, locked from sys_user where id=?";
        List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class), userId);
        if (userList.size() == 0) {
            return null;
        }
        return userList.get(0);
    }

    /**
     * 功能描述：查找所有用户
     *
     * @return List<User> 用户列表
     * @author cakin
     * @date 2020/5/19
     */
    public List<User> findAll() {
        String sql = "select id, organization_id, username, password, salt, role_ids as roleIdsStr, locked from sys_user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class));
    }

    /**
     * 功能描述：根据用户名查找用户
     *
     * @param username 用户名
     * @return User 用户
     * @author cakin
     * @date 2020/5/19
     */
    public User findByUsername( String username ) {
        String sql = "select id, organization_id, username, password, salt, role_ids as roleIdsStr, locked from sys_user where username=?";
        List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class), username);
        if (userList.size() == 0) {
            return null;
        }
        return userList.get(0);
    }
}
